package com.softserveinc.moviedbapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.softserveinc.moviedbapp.R;
import com.softserveinc.moviedbapp.model.Movie;

import java.util.ArrayList;

public class MovieAdapter extends ArrayAdapter<Movie> {

	private ArrayList<Movie> movies;

	public MovieAdapter(final Context context, final int textViewResourceId, final ArrayList<Movie> movies) {
		super(context, textViewResourceId, movies);
		this.movies = movies;
	}

	public static class ViewHolder {
		public TextView movieName;
		public TextView movieReleaseDate;
		public TextView movieVotes;
		public RatingBar movieRatingBar;
		public ImageView movieCertification;
		public ImageView moviePoster;
		public ProgressBar moviePosterProgress;
	}

	@Override
	public View getView(final int position, final View convertView, final ViewGroup parent) {
		View row = convertView;
		ViewHolder viewHolder;

		/* populate each row of the list */
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			row = inflater.inflate(R.layout.list_row, null);

			/* use view holder so we don't have to findViewById for every single view always */
			viewHolder = new ViewHolder();
			viewHolder.movieName = (TextView) row.findViewById(R.id.movieName);
			viewHolder.movieReleaseDate = (TextView) row.findViewById(R.id.releaseDate);
			viewHolder.movieVotes = (TextView) row.findViewById(R.id.votes);
			viewHolder.movieRatingBar = (RatingBar) row.findViewById(R.id.ratingBar);
			viewHolder.movieCertification = (ImageView) row.findViewById(R.id.certification);
			viewHolder.moviePoster = (ImageView) row.findViewById(R.id.poster);
			viewHolder.moviePosterProgress = (ProgressBar) row.findViewById(R.id.posterProgressBar);

			row.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) row.getTag();
		}

		Movie movie = movies.get(position);

		if (movie != null) {

			/* set the movie title */
			viewHolder.movieName.setText(movie.getName());

			/* set the release date */
			String dateReleased = movie.getReleased();
			if (dateReleased != null && !dateReleased.equals("1970-01-01")) {
				viewHolder.movieReleaseDate.setText(dateReleased);
			} else {
				viewHolder.movieReleaseDate.setText("N/A");
			}

			/* set the number of votes */
			viewHolder.movieVotes.setText(new Integer(movie.getVotes()).toString());

			/* set the rating bar value */
			viewHolder.movieRatingBar.setRating(((float) movie.getRating()) / 2);

			/* set the proper certification image */
			if (movie.getCertification().equals("PG-13")) {
				viewHolder.movieCertification.setImageResource(R.drawable.rating_pg13);
			} else if (movie.getCertification().equals("R")) {
				viewHolder.movieCertification.setImageResource(R.drawable.rating_r);
			} else if (movie.getCertification().equals("NR")) {
				viewHolder.movieCertification.setImageResource(R.drawable.rating_nr);
			} else if (movie.getCertification().equals("PG")) {
				viewHolder.movieCertification.setImageResource(R.drawable.rating_pg);
			} else if (movie.getCertification().equals("G")) {
				viewHolder.movieCertification.setImageResource(R.drawable.rating_g);
			} else {
				viewHolder.movieCertification.setImageResource(R.drawable.rating_nr);
			}

			/* set the poster url */
			Bitmap posterBitmap = movie.getPosterBitmap();
			if (posterBitmap != null) {
				viewHolder.moviePoster.setImageBitmap(posterBitmap);
				viewHolder.moviePoster.setVisibility(View.VISIBLE);
				viewHolder.moviePosterProgress.setVisibility(View.GONE);
			} else {
				viewHolder.moviePoster.setVisibility(View.GONE);
				viewHolder.moviePosterProgress.setVisibility(View.VISIBLE);
			}
		}
		return row;
	}

}
